<template>
  <div class="app-container">
    <SearchContent :queryParams="queryParams" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="里程(km):" prop="mileage">
          <el-select v-model="queryParams.mileage" clearable>
            <template v-for="dict in dict.type.mil_age">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="月份(月):" prop="months">
          <el-select v-model="queryParams.months" clearable>
            <template v-for="dict in dict.type.mon_ths">
              <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="调整系数:" prop="factor">
          <el-input placeholder="请输入调整系数" v-model="queryParams.factor" clearable></el-input>
        </el-form-item>
      </el-col>
    </SearchContent>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['enterprise:residualRateRegulation:add']">新增</el-button>
        <el-button type="primary" size="mini" @click="bHandleAdd" v-hasPermi="['enterprise:residualRateRegulation:batchAdd']">批量新增</el-button>
        <el-button type="primary" size="mini" @click="handleExport" v-hasPermi="['enterprise:residualRateRegulation:export']">导出</el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button v-hasPermi="['enterprise:residualRateRegulation:edit']" size="mini" type="text" @click="handleUpdate(scope.row)">编辑 </e-button>
              <el-button v-hasPermi="['enterprise:residualRateRegulation:void']" type="text" @click="voids(scope.row)">作废</el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column label="里程（km）" align="center" prop="mileage" />
          <el-table-column label="月份（月）" align="center" prop="months" />
          <el-table-column label="调整系数" align="center" prop="factor" />
        </template>
      </table-page>
    </div>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="40%">
      <div style="width: 60%; margin: 0 auto">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-form-item label="里程(km):" prop="mileage">
            <el-select v-model="form.mileage" clearable style="width: 100%">
              <template v-for="dict in dict.type.mil_age">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="月份(月):" prop="months">
            <el-select v-model="form.months" clearable style="width: 100%">
              <template v-for="dict in dict.type.mon_ths">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="调整系数:" prop="factor">
            <el-input placeholder="请输入调证系数" style="width: 100%" maxlength="10" v-model="form.factor" @input="form.factor = form.factor.replace(/^\D*([0-9]\d*\.?\d{0,5})?.*$/, '$1')" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SearchContent from '@/components/SearchContent/index.vue'
import { getRidList, ridAdd, ridUpdate, ridDelete, ridDetail, COEFFICIENTiMPORT, TEMPLATEDRIVER } from '@/api/globalField'

export default {
  name: 'ResidualRateRegulation',
  components: {
    SearchContent,
  },
  dicts: ['mon_ths', 'mil_age'],
  data() {
    return {
      rules: {
        mileage: [{ required: true, message: '请选择里程', trigger: 'change' }],
        months: [{ required: true, message: '请选择月份(月)', trigger: 'change' }],
        factor: [{ required: true, message: '请输入调证系数', trigger: 'change' }],
      },
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      isNew: true,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dataList: [],
      // 表单参数
      form: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 导出按钮操作 */
    handleExport() {
      this.download_json(
        '/setting/residualValueAdjustmentFactor/export',
        {
          ...this.queryParams,
        },
        `残值率调证系数列表_${new Date().getTime()}.xlsx`
      )
    },
    voids(row) {
      this.$confirm('确认作废?', '作废', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          ridDelete(row.id).then((res) => {
            this.$message({
              type: 'success',
              message: '作废成功',
            })
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消作废',
          })
        })
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true
      getRidList(this.queryParams).then((res) => {
        if (res.code == '200') {
          const { rows, total } = res
          this.dataList = rows
          this.total = total
          this.loading = false
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
    },
    reset() {
      this.form = {}
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      ;(this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.handleQuery()
    },
    /** 新增操作 */
    handleAdd() {
      this.open = true
      this.title = '新增'
      this.$refs['form'] && this.$refs['form'].resetFields()
      this.reset()
    },
    /** 批量新增操作 */
    bHandleAdd() {
      this.$UploadDialog({
        title: '残值率调整系数',
        showtoastSureBtn: false,
        showResult: false,
        uploadConfig: {
          uploadFileUrl: `#${COEFFICIENTiMPORT}`,
          limit: 10,
          params: {},
          fileType: ['xls', 'xlsx'],
          templateUrl: `${TEMPLATEDRIVER('residualValueAdjustmentFactor')}`,
          templateName: `"残值率调整系数-导入模版.xlsx`,
        },
      })
        .then((res) => {
          this.handleQuery()
        })
        .catch((err) => {
          this.handleQuery()
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.open = true
      this.title = '编辑'
      ridDetail(row.id).then((res) => {
        this.form = res.data
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            ridAdd(this.form).then((res) => {
              this.open = false
              this.getList()
              this.$message.success('新增成功')
            })
          } else {
            // 调取接口
            ridUpdate(this.form).then((res) => {
              this.open = false
              this.getList()
              this.$message.success('编辑成功')
            })
          }
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: '';
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
